package com.guigu.pd.auth.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.auth.entity.SsqEntity;
import com.guigu.pd.auth.service.SsqService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:05:57
 */
@RestController
@RequestMapping("auth/ssq")
public class SsqController {
    @Autowired
    private SsqService ssqService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("auth:ssq:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = ssqService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("auth:ssq:info")
    public R info(@PathVariable("id") Integer id){
		SsqEntity ssq = ssqService.getById(id);

        return R.ok().put("ssq", ssq);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("auth:ssq:save")
    public R save(@RequestBody SsqEntity ssq){
		ssqService.save(ssq);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("auth:ssq:update")
    public R update(@RequestBody SsqEntity ssq){
		ssqService.updateById(ssq);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("auth:ssq:delete")
    public R delete(@RequestBody Integer[] ids){
		ssqService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/getaddress/{pid}")
    public R getAddressbypid(@PathVariable("pid") Integer pid){
        List<SsqEntity> chinaAddresses = ssqService.getallProvince(pid);
        return R.ok().put("address",chinaAddresses);
    }

    @GetMapping("/getnamebyid/{id}")
    public String getnamebyId(@PathVariable("id") Integer id){
        SsqEntity byId = ssqService.getById(id);
        return byId.getName();
    }

    @GetMapping("/getprovince/{pid}")
    public List<String> province(@PathVariable("pid") Integer pid){
        QueryWrapper<SsqEntity> queryWrapper = new QueryWrapper<SsqEntity>();
        queryWrapper.eq("pid", 0);
        List<SsqEntity> chinaAddresses = ssqService.list(queryWrapper);
        List<String> provinces = new ArrayList<String>();
        for (SsqEntity address : chinaAddresses) {
            provinces.add(address.getName());
        }
        return provinces;
    }

}
